package com.easyshopping.facade;

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;

import com.easyshopping.ShoppingListActivity;
import com.easyshopping.domain.Currency;
import com.easyshopping.domain.Item;
import com.easyshopping.domain.ItemCategory;
import com.easyshopping.domain.ShoppingList;
import com.easyshopping.domain.Unit;
import com.easyshopping.persistence.IPersistence;
import com.easyshopping.persistence.SQLitePersistence;

public class ShoppingListFacade {
	
	private IPersistence persistence;
	
	private Context ctx;
	
	private ShoppingList currentSL;
	
	
	public ShoppingList getCurrentSL() {
		return currentSL;
	}

	public void setCurrentSL(ShoppingList currentSL) {
		this.currentSL = currentSL;
	}

	private static ShoppingListFacade instance = new ShoppingListFacade();
	
	public static ShoppingListFacade getInstance() {
		return instance;
	}

	public void showShoppingList(int shoppinglist_id, String shoppinglist_name) {
		
		ShoppingList sl = new ShoppingList(shoppinglist_id, shoppinglist_name);
		this.setCurrentSL(sl);
		
		Intent intent = new Intent(ctx, ShoppingListActivity.class);
		ctx.startActivity(intent);
	}
	
	public void addShoppingList() {
		
		ShoppingList sl = new ShoppingList();
		this.setCurrentSL(sl);
		this.getPersistence().saveShoppingList(sl);
		
		Intent intent = new Intent(ctx, ShoppingListActivity.class);
//		intent.putExtra(ShoppingListActivity.SLID, sl.getId());
//		intent.putExtra(ShoppingListActivity.SLNAME, sl.getName());
		ctx.startActivity(intent);
	}
	
	public void addItem(String itemText, int cat_id) {
		
		Item item  = new Item(itemText);
		item.setShoppingList(this.getCurrentSL());
		
//		if (cat_id != -1)
		item.setCat_id(cat_id);
		item.setCur_id(Currency.NO_CURRENCY);
		item.setUnit_id(Unit.NO_UNIT);
		
		this.getCurrentSL().addItem(item);
		this.getPersistence().saveItem(item);
	}
	
	public void addItemCategory(String itemCategory){
		
		ItemCategory cat = new ItemCategory(itemCategory);
		this.getPersistence().saveItemCategory(cat);
	}
			
	public void updateItemState(int item_id, boolean state) {
		
		this.getPersistence().updateItemState(item_id, state);
	}
	
	public void deleteItem(int item_id) {
		this.getPersistence().deleteItem(item_id);
	}
	
	public void deleteShoppinglist(int sl_id) {
		this.getPersistence().deleteShoppinglist(sl_id);
	}
	
	public void deleteItemCategory(int itemcate_id){
		this.getPersistence().deleteItemCategory(itemcate_id);
	}
	
	public boolean havingItemsInCategory(int itemcate_id) {
		
		Cursor c = this.getPersistence().fetchAllItemsOfCategory(itemcate_id);
		return c.getCount() != 0;
	}
	
	public Item fetchItem(int item_id) {

		Cursor c = this.getPersistence().fetchItem(item_id);
		c.moveToNext();
		
		Item.getInstance().setId(item_id);
		Item.getInstance().setName(c.getString(c.getColumnIndex(Item.ITEM_NAME)));
		Item.getInstance().setAmount(c.getDouble(c.getColumnIndex(Item.ITEM_AMOUNT)));
		Item.getInstance().setPrice(c.getDouble(c.getColumnIndex(Item.ITEM_PRICE)));
		Item.getInstance().setCat_id(c.getInt(c.getColumnIndex(Item.ITEM_CAT)));
		Item.getInstance().setCur_id(c.getInt(c.getColumnIndex(Item.ITEM_CUR)));
		Item.getInstance().setUnit_id(c.getInt(c.getColumnIndex(Item.ITEM_UNIT)));
		
		return Item.getInstance();
	}
	
	public ItemCategory fetchItemCategory(int itemCate_id){
		
		Cursor c = this.getPersistence().fetchItemCategory(itemCate_id);
		c.moveToNext();
		
		ItemCategory.getInstance().setId(itemCate_id);
		ItemCategory.getInstance().setName(c.getString(c.getColumnIndex(ItemCategory.ITEMCATEGORY_NAME)));
		
		return ItemCategory.getInstance();
	}
	
	public void updateItem(Item item) {
		
		ContentValues initialValues = new ContentValues();
		initialValues.put(Item.ITEM_NAME, item.getName());
		initialValues.put(Item.ITEM_AMOUNT, item.getAmount());
		initialValues.put(Item.ITEM_PRICE, item.getPrice());
		initialValues.put(Item.ITEM_CAT, item.getCat_id());
		initialValues.put(Item.ITEM_CUR, item.getCur_id());
		initialValues.put(Item.ITEM_UNIT, item.getUnit_id());
		
		this.getPersistence().updateItem(item.getId(), initialValues);
	}
	
	public void updateShoppinglist(ShoppingList sl) {
		
		ContentValues initialValues = new ContentValues();
		initialValues.put(ShoppingList.SL_NAME, sl.getName());
		
		this.getPersistence().updateShoppinglist(sl.getId(), initialValues);
	}
	
	public void updateItemCategory(ItemCategory itemCate){
		
		ContentValues initialValues = new ContentValues();
		initialValues.put(ItemCategory.ITEMCATEGORY_NAME, itemCate.getName());
		
		this.getPersistence().updateItemCategory(itemCate.getId(), initialValues);
	}
	
	
	public IPersistence getPersistence() {
		
		if (persistence == null) {
			persistence = new SQLitePersistence(this.getCtx());
			persistence.open();
		}
		
		return persistence;
	}

	public void setPersistence(IPersistence persistence) {
		this.persistence = persistence;
	}

	public Context getCtx() {
		return ctx;
	}

	public void setCtx(Context ctx) {
		this.ctx = ctx;
	}
}
